package cn.engage.org.controller;

import cn.engage.org.service.ShopService;
import cn.engage.org.domain.Shop;
import cn.engage.org.query.ShopQuery;
import cn.engage.basic.util.AjaxResult;
import cn.engage.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    public ShopService shopService;

    /**
     * 接口：添加或修改
     * @param shop  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.add(shop);
            return new AjaxResult().me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().me().fail();
        }
    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.delete(id);
            return new AjaxResult().me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().me().fail();
        }
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
                shopService.patchDelete(ids);
            return new AjaxResult().me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().me().fail();
        }
    }

    /**
    * 接口：查询单个对象
    * @param id
    */


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<Shop> findAll(){
        return shopService.findAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public PageList<Shop> queryByPage(@RequestBody ShopQuery query) {
        return shopService.queryByPage(query);
    }

    /* 店铺入驻*/
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        shopService.settlement(shop);
        return new AjaxResult().me().success();
    }

}
